package com.ichina.chat.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ichina.chat.dao.ServerDao;
import com.ichina.chat.vo.Server;

@Component
@Transactional
public class ServerDaoimpl implements ServerDao {
	private Server server;
	@Resource
	SessionFactory sessionFactory;
	public boolean addServer(Server server) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean delServer(int sid) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public List<Server> getAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public int getCount() throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}
	public Server loadServerById(int sid) throws Exception {
		server = (Server)sessionFactory.getCurrentSession().get(Server.class, sid);
		return server;
	}

	public boolean updateServer(Server server) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean login(String sname) throws Exception {
		server = (Server)sessionFactory.getCurrentSession().createQuery("from Server where sname=? ").setString(0, sname).uniqueResult();
		if(server != null){
			server.setState(1);
			this.updateServer(server);
			return true;
		}
		return false;
	}

	public boolean logout(String sname) throws Exception {
		server = (Server)sessionFactory.getCurrentSession().createQuery("from Server where sname=?").setString(0, sname).uniqueResult();
		if(server != null){
			server.setState(0);
			this.updateServer(server);
			return true;
		}
		return false;
	}

}
